package club.ruanx.movie.controller;

import club.ruanx.acl.entity.SysParam;
import club.ruanx.acl.service.SysParamService;
import club.ruanx.bean.PageBean;
import club.ruanx.core.exception.TargetEntityNotFound;
import club.ruanx.movie.common.constraint.MovieParamConstraint;
import club.ruanx.movie.entity.Person;
import club.ruanx.movie.param.PersonParam;
import club.ruanx.movie.service.PersonService;
import club.ruanx.movie.util.ParamUtils;
import club.ruanx.movie.vo.PersonVO;
import club.ruanx.util.BeanUtils;
import club.ruanx.util.CollectionUtils;
import club.ruanx.util.PagingUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 阮胜
 * @date 2018/10/22 14:52
 */
@RestController
@RequestMapping("/person")
public class PersonController {
    private final PersonService personService;
    private final SysParamService sysParamService;

    public PersonController(PersonService personService, SysParamService sysParamService) {
        this.personService = personService;
        this.sysParamService = sysParamService;
    }

    @GetMapping("/{id}")
    public Person findById(@PathVariable String id) throws TargetEntityNotFound {
        return personService.findById(id);
    }

    @GetMapping("/list")
    public List<Person> list(PageBean pageBean) {
        Page<Person> personPage = personService.findAll(pageBean);
        PagingUtils.setNgxAdminPagingHeader(personPage.getTotalElements());
        return personPage.getContent();
    }

    @GetMapping("/detail/{id}")
    public PersonVO findDetailById(@PathVariable String id) throws TargetEntityNotFound {
        Person person = personService.findById(id);
        List<SysParam> paramList = sysParamService.findAllByFlag(MovieParamConstraint.PERSON_PROFESSION);
        return parseToPersonVO(paramList, person);
    }

    @GetMapping("/{profession}/vo/list")
    public List<PersonVO> findByProfession(@PathVariable Integer profession, PageBean pageBean) {
        Page<Person> personPage = personService.findByProfession(profession, pageBean);
        PagingUtils.setNgxAdminPagingHeader(personPage.getTotalElements());
        return fillData(personPage.getContent());
    }

    @GetMapping("/allProfession/vo/list")
    public List<PersonVO> findAllProfession(PageBean pageBean) {
        Page<Person> personPage = personService.findAllWherePhotosNotEmpty(pageBean);
        PagingUtils.setNgxAdminPagingHeader(personPage.getTotalElements());
        return fillData(personPage.getContent());
    }

    private List<PersonVO> fillData(List<Person> personList) {
        if (CollectionUtils.isEmpty(personList)) {
            return null;
        }
        List<SysParam> paramList = sysParamService.findAllByFlag(MovieParamConstraint.PERSON_PROFESSION);
        return personList.stream().map(person -> parseToPersonVO(paramList, person)).collect(Collectors.toList());
    }

    private PersonVO parseToPersonVO(List<SysParam> paramList, Person person) {
        List<String> professionList = JSON.parseArray(person.getProfessions(), String.class);
        String[] professions = ParamUtils.parseFieldValue(professionList, paramList);
        PersonVO personVO = new PersonVO();
        BeanUtils.copyPropertiesIgnoreNull(person, personVO);
        personVO.setProfessions(professions);
        List<String> photos = JSON.parseArray(person.getPhotos(), String.class);
        personVO.setPhotos(photos);
        return personVO;
    }

    @PostMapping("/save")
    public Person save(@RequestBody @Valid PersonParam personParam) {
        return personService.save(personParam);
    }

    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable String id) throws Exception {
        personService.delete(id);
    }

    @DeleteMapping("/delete")
    public List<String> deleteAll(String[] ids) {
        return personService.deleteIn(ids);
    }
}
